/*
TomorIT: a general data compression project aimed to be quick and efficient,
by Arpad Fekete, in Hungarian Fekete Árpád, born: Kiskunfélegyháza, 1982.04.28.
homepages: http://mathboasting.blogspot.com, http://fecowebs.ourproject.org,
motivation: Árpád aims to re-learn C++ and maybe enter the Hutter Prize,
and collaboration will be possible if the first official release is ready...

The code is shared by the MIT License:

Copyright (c) 2014 Arpad Fekete

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--- In this (header + class) ---
Base class for converting a raw character stream or a raw binary stream to a
sequence of numbers, by departing the stream into tokens or words or single
bytes, and assigning an integer number to each of the tokens or words or single
bytes. So, this should convert the input from a physical state to a
mathematical model. Ideally, this operation should not consume too much memory,
so the instances of tokens should not be memorized by the program, but computed
real-time instead, converting the byte stream to a stream of integer numbers.

Although this class could have been abstract, let's give it a trivial
implementation... Although this is like a header file, for the templates it
should also carry its implementation for technical reasons, and maybe the
easiest way of doing that is by not using a header file at all.
*/
#include <istream>

template<typename IntegerType> class ToSequence {
public:
    // constructor
    ToSequence(std::istream &is);

    // at least we should have a method to read the meta-stream entry by entry
    // if there is something to read, read it, otherwise the return value is unspecified
    // let's introduce a method getNextSuccess()
    virtual IntegerType getNext();

    // tells whether the getNext() operation has returned successfully
    virtual bool getNextSuccess();

    // it is not quick to read from disk and write to another part of the disk
    // at the same time, so storing some things in memory is beneficial anyway
    // so we shall have a method similar to "istream::read", making the
    // converting process much more efficient
    virtual int getNext(IntegerType*, int);

protected:
    // this is the same for binary or text files
    std::istream* inputstream;
    bool bool_getNextSuccess;
};

template<typename IntegerType> ToSequence<IntegerType>::ToSequence(std::istream &is) {
    inputstream = &is;
    bool_getNextSuccess = false;
}

template<typename IntegerType> IntegerType ToSequence<IntegerType>::getNext() {
    IntegerType data;
    inputstream->read(&data, 1);
    if (*inputstream) {
        // data read successfully
        bool_getNextSuccess = true;
        return data;
    }
    bool_getNextSuccess = false;

    // the return value is unspecified, let's hope the compiler allows it
    return data;
}

template<typename IntegerType> bool ToSequence<IntegerType>::getNextSuccess() {
    return bool_getNextSuccess;
}

template<typename IntegerType> int ToSequence<IntegerType>::getNext(IntegerType* it, int in) {
    // TODO: implement!
    // for now, we shall only give back one

    // now, each subclass should have an IntegerType associated,
    // and ToSequence will have "char", so it won't be a problem here
    // this is the most basic kind of implementation
    IntegerType data;
    inputstream->read(&data, 1);
    if (*inputstream) {
        // data read successfully
        it = &data;
        return 0;
    }
    // inputstream->gcount() tells how many characters are read
    // but in case of 1 piece of data, we should "do nothing"
    return 1;
}

